import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import DTO.LocaldataDTO;
import Entity.Localdata;
import Method.Queryuserid;

import javax.servlet.ServletException;
@WebServlet(urlPatterns = {"/dgrep"})
public class DgrepServlet extends HttpServlet{
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            String new_password = request.getParameter("new_password");
            LocaldataDTO  localdataDTO=(LocaldataDTO) request.getSession().getAttribute("localdataDTO");
            tx.begin();
            String arr[]=new String[7];
            Queryuserid queryuserid=new Queryuserid();
            arr=queryuserid.execute(localdataDTO.getUserid());
            Localdata localdata=em.find(Localdata.class,Integer.valueOf(arr[6]));
            localdata.setPassword(new_password);
            em.persist(localdata);
            tx.commit();
            localdataDTO=new LocaldataDTO(localdataDTO.getUserid(),new_password,localdataDTO.getLocalname(),localdataDTO.getPhone(),localdataDTO.getDgutid(),localdataDTO.getImageurl());
            request.getSession().setAttribute("localdataDTO",localdataDTO);
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            em.close();
        }
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("修改密码成功");
        response.sendRedirect("/HomePage.jsp");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
}